package array

// [0, 1, 2, 3, 4, 5]

func search(nums []int, target int) int {
	left := 0
	right := len(nums) - 1
	for left < right {
		mid := right - (right-left)/2
		if nums[mid] == target {
			return mid
		} else if nums[mid] < target {
			left = mid + 1
		} else {
			right = mid - 1
		}
	}
	return -1
}

// 左闭右开写法
func search2(nums []int, target int) int {
	var left int = 0
	var right int = len(nums)
	for (left < right) {
		mid := left + ((right - left) >> 1)
		if nums[mid] > target {
			right = mid
		} else if nums[mid] < target {
			left = mid + 1
		} else {
			return mid
		}
	}
	// 未找到
	return -1
}


